<?
$r = "../";
require_once($r."_engine/header_nocache.php");
require_once($r."_config/db.php");
require_once($r."_config/site.php");
require_once($r."_engine/functions.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_engine/c_resources_storage.php");
require_once($r."_engine/c_monitor.php");
require_once($r."_engine/c_account.php");

require_once($r."_inc/c_table_generator2.php");

function calc_percents($a, $b)
{
	if ($b == 0) return 0;
	return round(($a / $b) * 100);
}
function num_to_percents($a, $b)
{
	if ($b == 0) return "";
	return calc_percents($a, $b)."%";
}
function month_to_str($m)
{
	return date("F", mktime(0,0,0,$m,1,2000));
}

$title = "Misc Stats";
include "_header.php";
?>



<p class="redHeading">Num of changes per week
<?
$a = array();
$i_max = 0;
for ($i = 0; $i <= 6; $i++)
{
	$row = @mysql_fetch_array(execute_query("
		SELECT
			COUNT(*) AS cnt
		FROM
			download_stats
		WHERE
			DATE_FORMAT(download_dt, '%w') = '$i' AND (
			resource_status = ".RESOURCE_STORAGE_STATUS_CHANGED." OR
			resource_status = ".RESOURCE_STORAGE_STATUS_TRIGGERED.")
	"));
	$a[$i]['chng'] = $row["cnt"];
	
	$row = @mysql_fetch_array(execute_query("
		SELECT
			COUNT(*) AS cnt
		FROM
			download_stats
		WHERE
			DATE_FORMAT(download_dt, '%w') = '$i'
	"));
	$a[$i]['total'] = $row["cnt"];
	
	if (calc_percents($a[$i]["chng"], $a[$i]["total"]) >
		calc_percents($a[$i_max]["chng"], $a[$i_max]["total"]))
	{
		$i_max = $i;
	}
}

echo '<br>Most favorite day of the week (0=Sunday..6=Saturday): '.$i_max;

$tg = new CTableGenerator2(1, true);
$tg->td_class["default"] = 'class="textStd"';

$tg->PrintHeader();

$tg->Cell("Day of the week","header|begin");
$tg->Cell("Changed", 		"header");
$tg->Cell("Total", 			"header");
$tg->Cell("Changed %", 		"header|end");

foreach ($a as $i => $v)
{
	$tg->Cell($i,						"begin");
	$tg->Cell($v["chng"],				"");
	$tg->Cell($v["total"],				"");
	$tg->Cell(num_to_percents($v["chng"], $v["total"]), "end");
}

$tg->PrintFooter();
?>



<p class="redHeading">Num of changes per month
<?
$a = array();
$m_max = 1;
for ($i = 1; $i <= 12; $i++)
{
	$s = str_pad($i, 2, "0", STR_PAD_LEFT);
	
	$row = @mysql_fetch_array(execute_query("
		SELECT
			COUNT(*) AS cnt
		FROM download_stats
		WHERE
			download_dt LIKE '%-$s-%' AND (
			resource_status = ".RESOURCE_STORAGE_STATUS_CHANGED." OR
			resource_status = ".RESOURCE_STORAGE_STATUS_TRIGGERED.")
	"));
	$a[$i]['chng'] = $row["cnt"];
	
	$row = @mysql_fetch_array(execute_query("
		SELECT
			COUNT(*) AS cnt
		FROM download_stats
		WHERE
			download_dt LIKE '%-$s-%'
	"));
	$a[$i]['total'] = $row["cnt"];
	
	if (calc_percents($a[$i]["chng"], $a[$i]["total"]) >
		calc_percents($a[$m_max]["chng"], $a[$m_max]["total"]))
	{
		$m_max = $i;
	}
}

echo '<br>Most favorite month: '.month_to_str($m_max);

$tg = new CTableGenerator2(1, true);
$tg->td_class["default"] = 'class="textStd"';

$tg->PrintHeader();

$tg->Cell("Month",		 	"header|begin");
$tg->Cell("Changed", 		"header");
$tg->Cell("Total", 			"header");
$tg->Cell("Changed %", 		"header|end");

foreach ($a as $m => $v)
{
	$s = date("F", mktime(0,0,0,$m,1,2000));
	$tg->Cell($s,						"begin");
	$tg->Cell($v["chng"],				"");
	$tg->Cell($v["total"],				"");
	$tg->Cell(num_to_percents($v["chng"], $v["total"]), "end");
}

$tg->PrintFooter();
?>



<p class="redHeading">Favorite Icon
<?
$row = @mysql_fetch_array(execute_query("
SELECT 
	COUNT(*) AS cnt
FROM monitors
WHERE favicon IS NOT NULL
"));
$c1 = $row["cnt"];

$row = @mysql_fetch_array(execute_query("
SELECT 
	COUNT(*) AS cnt
FROM monitors
"));
$c3 = $row["cnt"];

$c2 = $c3 - $c1;


$tg = new CTableGenerator2(1, true);
$tg->td_class["default"] = 'class="textStd"';

$tg->PrintHeader();

$tg->Cell("Presented", 		"header|begin");
$tg->Cell("Not Presented",	"header|end");

$tg->Cell($c1,				"begin");
$tg->Cell($c2,	 			"end");

$tg->Cell(num_to_percents($c1, $c3),	"begin");
$tg->Cell(num_to_percents($c2, $c3),	"end");

$tg->PrintFooter();


include "_footer.php";
?>
